home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Ph 1.1.1 / Lib / ldf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  5.8 KB  |  199 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     ldf.c - List Definition Procedure for the Report Module.
  4.     
  5.     Copyright © 1988-1991 Northwestern University.
  6.  
  7.     This LDEF is used by type 1 reports.  It ignores all but draw
  8.     messages.  The cell data specifies the index in the auxiliary array
  9.     of a handle to an STR# resource, and the offset of the line within
  10.     the resource.
  11. _____________________________________________________________________*/
  12.  
  13. #pragma load "precompile"
  14. #include "doc.h"
  15.  
  16. pascal void main (short lMessage, Boolean lSelect, 
  17.     Rect *lRect, Cell *lCell, short lDataOffset,
  18.     short lDataLen, ListHandle lHandle)
  19.     
  20. {
  21. #pragma unused (lSelect, lCell, lDataLen)
  22.  
  23.     auxInfo            **aux;            /* handle to auxiliary info */
  24.     Handle            theCells;        /* handle to cell data */
  25.     unsigned char    *p;                /* pointer to cell data */
  26.     Handle            theStrings;        /* handle to STR# resource */
  27.     unsigned short    offset;            /* offset of line in STR# resource */
  28.     unsigned char    *theLine;        /* pointer to the line */
  29.     unsigned char    *q;                /* pointer to cur pos in the line */
  30.     unsigned char    *qEnd;            /* pointer to end of line */
  31.     short                nchar;            /* number of chars to draw */
  32.     short                baseLine;        /* base line for text */
  33.     Boolean            escStyle;        /* true if style escape sequence */
  34.     Boolean            escJust;            /* true if just escape sequence */
  35.     Boolean            escPict;            /* true if pict escape sequence */
  36.     unsigned char    styleCode;        /* style */
  37.     unsigned char    justCode;        /* justification */
  38.     short                cellHeight;        /* cell height */
  39.     short                cellWidth;        /* cell width */
  40.     short                picID;            /* pict resource id */
  41.     short                picBand;            /* pict band number */
  42.     PicHandle        picHandle;        /* handle to pict */
  43.     short                picWidth;        /* pict width */
  44.     short                picHeight;        /* pict height */
  45.     Rect                picRect;            /* pict offscren rectangle */
  46.     Rect                picSrcRect;        /* CopyBits source rect */
  47.     Rect                picDstRect;        /* CopyBits dest rect */
  48.     GrafPort            picPort;            /* grafport for offscreen PICT drawing */
  49.     BitMap            picMap;            /* bitmap for offscreen PICT drawing */
  50.     
  51.     /* Get pointer to the line to be drawn. */
  52.     
  53.     if (lMessage != lDrawMsg) return;
  54.     aux = (auxInfo**)(**lHandle).userHandle;
  55.     theCells = (**lHandle).cells;
  56.     p = (unsigned char*)*theCells + lDataOffset;
  57.     theStrings = (**aux).auxArray[*p++];
  58.     offset = (*p << 8) | *(p+1);
  59.     if (!*theStrings) LoadResource(theStrings);
  60.     HLock(theStrings);
  61.     theLine = (unsigned char*)*theStrings + offset;
  62.     
  63.     /* Get escape sequence info. */
  64.     
  65.     escStyle = escJust = escPict = false;
  66.     q = theLine+1;
  67.     qEnd = q + *theLine;
  68.     while (q < qEnd && *q < 31) {
  69.         switch (*q) {
  70.             case docStyle:
  71.                 escStyle = true;
  72.                 styleCode = *(q+2);
  73.                 break;
  74.             case docJust:
  75.                 escJust = true;
  76.                 justCode = *(q+2);
  77.                 break;
  78.             case docPict:
  79.                 escPict = true;
  80.                 picID = *(q+2)<<8 | *(q+3);
  81.                 picBand = *(q+4)<<8 | *(q+5);
  82.                 break;
  83.         }
  84.         q += *(q+1);
  85.     }
  86.                     
  87.     if (escPict) {
  88.     
  89.         /* Draw a picture. */
  90.         
  91.         cellWidth = lRect->right - lRect->left;
  92.         cellHeight = (**lHandle).cellSize.v;
  93.         
  94.         if ((**aux).cachedPictID != picID) {
  95.         
  96.             /* This picture is not cached - we must cache it in an offscreen
  97.                 bitmap. */
  98.             
  99.             /* Dispose of any previously cached bitmap. */
  100.             
  101.             if ((**aux).cachedPictID) DisposPtr((**aux).cachedBitMap.baseAddr);
  102.             
  103.             /* Compute picRect = the bounds rectangle for the cached
  104.                 picture. */
  105.             
  106.             picHandle = GetPicture(picID);
  107.             if (!picHandle) {
  108.                 HUnlock(theStrings);
  109.                 return;
  110.             }
  111.             if (!*picHandle) LoadResource((Handle)picHandle);
  112.             HLock((Handle)picHandle);
  113.             picWidth = (**picHandle).picFrame.right -
  114.                 (**picHandle).picFrame.left;
  115.             picHeight = (**picHandle).picFrame.bottom -
  116.                 (**picHandle).picFrame.top;
  117.             if (!escJust) justCode = docCenter;
  118.             switch (justCode) {
  119.                 case docLeft:
  120.                     picRect.left = lRect->left + 4;
  121.                     break;
  122.                 case docCenter:
  123.                     picRect.left = lRect->left + 
  124.                         ((cellWidth - picWidth)>>1);
  125.                     break;
  126.                 case docRight:
  127.                     picRect.left = lRect->right - 4 - picWidth;
  128.                     break;
  129.             }
  130.             picRect.right = picRect.left + picWidth;
  131.             picRect.top = 0;
  132.             picRect.bottom = picHeight;
  133.     
  134.             /* Allocate and initialize the offscreen  bitmap. */
  135.             
  136.             (**aux).cachedPictID = picID;
  137.             picMap.bounds = picRect;
  138.             picMap.rowBytes = (((picWidth+7)>>3) + 1) & 0xfffe;
  139.             picMap.baseAddr = NewPtr(picMap.rowBytes*picHeight);
  140.             (**aux).cachedBitMap = picMap;
  141.             
  142.             /* Draw the picture in the offscreen bitmap. */
  143.             
  144.             OpenPort(&picPort);
  145.             SetPortBits(&picMap);
  146.             picPort.portRect = picRect;
  147.             RectRgn(picPort.visRgn, &picRect);
  148.             ClipRect(&picRect);
  149.             EraseRect(&picRect);
  150.             DrawPicture(picHandle, &picRect);
  151.             HUnlock((Handle)picHandle);
  152.             ClosePort(&picPort);
  153.             SetPort((**lHandle).port);
  154.         }
  155.  
  156.         /* CopyBits the proper band from the offscreen cached bitmap to
  157.             the cell. */
  158.             
  159.         picMap = (**aux).cachedBitMap;
  160.         picSrcRect = picMap.bounds;
  161.         picSrcRect.top = picBand*cellHeight;
  162.         picSrcRect.bottom = picSrcRect.top + cellHeight;
  163.         if (picSrcRect.bottom > picMap.bounds.bottom) 
  164.             picSrcRect.bottom = picMap.bounds.bottom;
  165.         picDstRect = picSrcRect;
  166.         picDstRect.top = lRect->top;
  167.         picDstRect.bottom = picDstRect.top + picSrcRect.bottom - picSrcRect.top;
  168.         CopyBits(&picMap, &(**lHandle).port->portBits, &picSrcRect, &picDstRect, 
  169.             srcCopy, nil);
  170.         
  171.     } else {
  172.     
  173.         /* Draw a text line. */
  174.     
  175.         if (!escStyle) styleCode = normal;
  176.         if (!escJust) justCode = docLeft;
  177.         TextFace(styleCode);
  178.         nchar = *theLine - (q - theLine - 1);
  179.         if (nchar && *(q+nchar-1) == docEop) nchar--;
  180.         baseLine = lRect->bottom - 2;
  181.         switch (justCode) {
  182.             case docLeft:
  183.                 MoveTo(lRect->left + 4, baseLine);
  184.                 break;
  185.             case docCenter:
  186.                 MoveTo((lRect->left + lRect->right - TextWidth(q, 0, nchar)) >> 1,
  187.                     baseLine);
  188.                 break;
  189.             case docRight:
  190.                 MoveTo(lRect->right - 4 - TextWidth(q, 0, nchar), baseLine);
  191.                 break;
  192.         }
  193.         DrawText(q, 0, nchar);
  194.         TextFace(normal);
  195.     }
  196.     
  197.     HUnlock(theStrings);
  198. }
  199.